<?php

namespace addons\qingdongams\controller;

use addons\qingdongams\library\Wechat;
use addons\qingdongams\library\wx\WxBizDataCrypt;
use addons\qingdongams\model\Contract;
use addons\qingdongams\model\Customer;
use addons\qingdongams\model\Flow;
use addons\qingdongams\model\Receivables;
use addons\qingdongams\model\Staff as StaffModel;
use addons\qingdongams\model\StaffDepartment;
use addons\qingdongams\model\StaffSignIn;
use addons\qingdongams\model\Achievement;
use addons\qingdongams\model\Workorder;
use addons\qingdongams\model\WorkReport;
use app\admin\model\AuthGroup;
use fast\Http;
use think\Exception;
use think\Log;
use think\Validate;

/**
 * @desc 操作文档：https://doc.fastadmin.net/qingdongams
 * @desc 软件介绍：https://www.fastadmin.net/store/qingdongams.html
 * @desc 售后微信：qingdong_crm
 */
/**
 * 员工登录 注册接口
 */
class Staff extends StaffApi
{
    protected $noNeedLogin = ['test', 'wxLogin','login','sendSms','mobileLogin','logintest'];
    protected $noNeedRight = ['getInfo', 'editInfo', "use_phone"];


    public function _initialize()
    {
        parent::_initialize();
    }


    //是否登录
    public function isLogin()
    {
        $this->success('已登录', ['userinfo' => $this->auth->getUserinfo()]);
    }

    //获取销售信息
    public function getStaffInfo()
    {
        $staff=StaffModel::where(['id'=>$this->auth->id])->find();
        $this->success('已登录', ['userinfo' => $staff]);
    }


    /**
     * 会员登录
     *
     * @ApiMethod (POST)
     * @param string $account 账号
     * @param string $password 密码
     */
    public function login()
    {
        $account = $this->request->post('account');
        $password = $this->request->post('password');
        if (!$account || !$password) {
            $this->error(__('Invalid parameters'));
        }
        $ret = $this->auth->login($account, $password);
        if ($ret) {
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success(__('Logged in successful'), $data);
        } else {
            $this->error($this->auth->getError());
        }
    }
    /**
     * 会员手机号登录
     *
     * @ApiMethod (POST)
     * @param string $account 账号
     * @param string $password 密码
     */
    public function mobileLogin()
    {
        $mobile = $this->request->post('mobile');
        $code = $this->request->post('code');
        $mobilecode = $this->request->post('mobilecode');
        if (empty($mobile)) {
            $this->error('手机号不能为空');
        }
        if (empty($code)) {
            $this->error('微信code不能为空');
        }
        $cacheCode = cache($mobile . '_code');
        if (empty($cacheCode)) {
            $this->error('请先发送验证码');
        }
        if ($mobilecode != $cacheCode) {
            $this->error('验证码错误');
        }
        $staff = StaffModel::get(['mobile' => $mobile]);
        if (!$staff) {
            $this->error('账户不正确');
        }
        if ($staff->status != 1) {
            $this->error('账户已经被锁定');
        }

        //直接登录会员
        $this->auth->direct($staff->id);
        $wechat = new Wechat('wxMiniProgram');
        $decryptSession = $wechat->code($code);
        if (isset($decryptSession['errcode']) && $decryptSession['errcode'] != 0) {
            $this->error(__('登录失败！'));
        }
        $openid = $decryptSession['openid'];
        $unionid = $decryptSession['unionid'] ?? '';
        if (empty($staff['openid'])) {
            $userInfo = input('userInfo/a', '');
            if (empty($userInfo)) {
                $this->error('参数错误');
            }
            $update = [];
            $update['openid'] = $openid;
            $update['unionid'] = $unionid;
            $update['img'] = $userInfo['avatarUrl'];
            $update['nickname'] = $userInfo['nickName'];
            $update['status'] = 0;
            StaffModel::where(['id' => $this->auth->id])->update($update);

            $this->success('绑定账号成功，等待管理员审核', ['userinfo' => $this->auth->getUserinfo()], 405);
        }
        if ($openid != $staff['openid']) {
            $this->error('绑定微信账号不一致');
        }
        $data = ['userinfo' => $this->auth->getUserinfo()];
        $this->success(__('Logged in successful'), $data);
    }

    /**
     * 获取个人基本信息
     */
    public function getInfo()
    {
        $staff = StaffModel::where(['id' => $this->auth->id])->with(['parent'])->find();

        $this->success('请求成功', $staff);
    }


    /**
     * 修改个人基本信息
     */
    public function editInfo()
    {
        $name = input('name');
        $img = input('img');
        $email = input('email');
        $sex = input('sex');
        $num = input('num');
        $post = input('post');

        if (StaffModel::where(['id' => $this->auth->id])->update([
                'name' => $name,
                'img' => $img,
                'email' => $email,
                'sex' => $sex,
                'num' => $num,
                'post' => $post,
                'updatetime' => time()
            ]) == false) {
            $this->error('修改失败');
        }
        $this->success('修改成功');
    }


    //绑定微信
    public function wechatBind(){
        $type=input('type');//applet小程序   web 网页授权
        $code = input('code');
        if($type == 'web'){
            $wechat = new Wechat('wxOfficialAccount');
            if(empty($wechat->config['app_id'])){
                $this->error('微信appid未设置');
            }
            $decryptSession = $wechat->getOfficialAccessToken($code);

            if (isset($decryptSession['errcode']) && $decryptSession['errcode'] != 0) {
                $this->error(__('解析失败！'));
            }
            $openid = $decryptSession['openid'];
            $unionid = $decryptSession['unionid'] ?? '';

            Log::info(json_encode($decryptSession,JSON_UNESCAPED_UNICODE));
            //判断用户是否存在
            $staff = StaffModel::where(['wx_openid' => $openid])->find();
            if($staff){
                $this->error('该微信号已绑定账号');
            }
            $staffModel=new StaffModel();
            $result=$staffModel->save(['wx_openid'=>$openid,'unionid'=>$unionid],['id'=>$this->auth->id]);
            if($result){
                $this->success('绑定成功');
            }
            $this->error('绑定失败');
        }elseif($type == 'applet'){
            $wechat = new Wechat('wxMiniProgram');
            if(empty($wechat->config['app_id'])){
                $this->error('微信appid未设置');
            }
            $decryptSession = $wechat->code($code);
            if (isset($decryptSession['errcode']) && $decryptSession['errcode'] != 0) {
                $this->error(__('解析失败！'));
            }
            $openid = $decryptSession['openid'];
            $unionid = $decryptSession['unionid'] ?? '';
            //判断用户是否存在
            $staff = StaffModel::where(['openid' => $openid])->find();
            if($staff){
                $this->error('该微信号已绑定账号');
            }
            $staffModel=new StaffModel();
            $result=$staffModel->save(['openid'=>$openid,'unionid'=>$unionid],['id'=>$this->auth->id]);
            if($result){
                $this->success('绑定成功');
            }
            $this->error('绑定失败');
        }
    }



    //解绑微信
    public function unboundWechat()
    {
        $staffId = $this->auth->id;
        $model = new StaffModel();
        $staff = $model->where(['id' => $staffId])->find();
        if (empty($staff)) {
            $this->error('员工不存在');
        }
        if ($model->isUpdate(true)->save(['id' => $staffId, 'wx_openid' => null,
            'unionid' => null, 'openid' => null])) {
            $this->success('解绑微信成功');
        }
        $this->error('操作失败');
    }
    //授权手机号
    public function use_phone()
    {
        $post = $this->request->post();
        $code = $post['code'];
        $encryptedData = $post['encryptedData'];
        $iv = $post['iv'];

        $config = new Wechat('wxMiniProgram');
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $config->config['app_id'] . "&secret=" . $config->config['secret'] . "&js_code=" . $code . "&grant_type=authorization_code";
        $user = Http::get($url);
        $arr = json_decode($user, true);
        if (isset($arr['errcode']) && $arr['errcode'] != 0) {
            $this->error('微信小程序appid或secret错误！');
        }
        if (!isset($arr['openid']) || !isset($arr['session_key'])) {
            $this->error('授权失败,请重新授权1！');
        }
        $openId = $arr['openid'];
        $pc = new WxBizDataCrypt($config->config['app_id'], $arr['session_key']);
        $errCode = $pc->decryptData($encryptedData, $iv, $data);
        if ($errCode != 0) {
            $this->error('授权失败,请重新授权2！');
        }
        $decryptUserInfo = json_decode($data, true);
        if (isset($decryptUserInfo['phoneNumber']) && !empty($decryptUserInfo['phoneNumber'])) {
            $user_id = $this->auth->id;
            $oldStaff= StaffModel::where(array('mobile' => $decryptUserInfo['phoneNumber']))->find();
            if($oldStaff && $oldStaff['id'] != $user_id){
               $this->error('手机号已存在！');
            }

           $oldStaff= StaffModel::where(array('mobile' => $decryptUserInfo['phoneNumber']))->find();
           if($oldStaff && empty($oldStaff['openid'])){
                StaffModel::where(['id'=>$oldStaff['id']])->update(['openid'=>$openId]);
           }
            $userInfo = StaffModel::where(array('id' => $user_id))->update(array('mobile' => $decryptUserInfo['phoneNumber']));
            if ($userInfo === false) {
                $this->error('绑定失败！');
            }
            $data = ['mobile' => $decryptUserInfo['phoneNumber']];
            $this->success('绑定成功', $data);
        } else {
            $this->error('授权失败！');
        }
    }
    /**
     * 修改密码
     */
    public function changepwd()
    {
        if ($this->request->isPost()) {
            $oldpassword = $this->request->post("oldpassword");
            $newpassword = $this->request->post("newpassword");
            $renewpassword = $this->request->post("renewpassword");
            $rule = [
                'oldpassword'   => 'require|length:6,30',
                'newpassword'   => 'require|length:6,30',
                'renewpassword' => 'require|length:6,30|confirm:newpassword',
            ];

            $msg = [
                'renewpassword.confirm' =>'两次输入的密码不一致'
            ];
            $data = [
                'oldpassword'   => $oldpassword,
                'newpassword'   => $newpassword,
                'renewpassword' => $renewpassword,
            ];
            $field = [
                'oldpassword'   => '旧密码',
                'newpassword'   => '新密码',
                'renewpassword' => '确认密码'
            ];
            $validate = new Validate($rule, $msg, $field);
            $result = $validate->check($data);
            if (!$result) {
                $this->error(__($validate->getError()));
                return false;
            }

            $ret = $this->auth->changepwd($newpassword, $oldpassword);
            if ($ret) {
                $this->success('修改密码成功');
            } else {
                $this->error($this->auth->getError());
            }
        }
    }

    /**
     * 退出登录
     */
    public function logout()
    {
        //退出本站
        $this->auth->logout();
        $this->success(__('Logout successful'), url('user/index'));
    }

    //获取下级员工列表
    public function getStaffList()
    {
        $ids = StaffModel::getLowerStaffId();

        $staff = StaffModel::where([
            'id' => ['in', $ids],
            'status' => 1
        ])->with(['parent'])->field('id,name,nickname,img,num,mobile,post,parent_id')->select();
        $this->success('请求成功', $staff);
    }
    //获取团队列表
    public function getStaffTeamList()
    {
        $name = input('name','');
        $ids = StaffModel::getMyStaffIds();
        $where=[];
        if($name){
            $where['name'] = array('like','%'.$name.'%');
        }
        $staff = StaffModel::where([
            'id' => ['in', $ids],
            'status' => 1
        ])->where($where)->with(['parent'])->field('id,name,nickname,img,num,mobile,post,parent_id')->select();
        $this->success('请求成功', $staff);
    }

    //获取员工详情
    public function getStaffDetail()
    {
        $id = input('id');
        if (empty($id)) {
            $this->error('员工不存在');
        }
        $staff = StaffModel::with([ 'parent'])->where([
            'id' => $id,
        ])->find();
        $roles = [1 => '最高管理员', 2 => '公司管理员', 3 => '部门经理', 4 => '部门主管', 5 => '组长', 6 => '员工'];
        $staff['role'] = $roles[$staff['role']] ?? '';
        $this->success('请求成功', $staff);
    }

    //待审核员工列表
    public function getCheckStaffList()
    {
        $staff = StaffModel::where([
            'status' => 0,
            
            'name' => ['neq', '']
        ])->field('id,name,nickname,img,num,mobile,post,status')->select();
        $this->success('请求成功', $staff);
    }

    //修改员工信息
    public function updateStaff()
    {
        $name = input('name', '');
        $img = input('img', '');
        $email = input('email', '');
        $sex = input('sex', 0);
        $num = input('num', '');
        $post = input('post', '');
        $department_id = input('department_id', 0);
        $parent_id = input('parent_id', 0);
        $id = input('id', 0, 'intval');

        if (empty($department_id)) {
            $this->error('请选择员工部门！');
        }
        if (StaffModel::where(['id' => $id])->update([
                'name' => $name,
                'img' => $img,
                'email' => $email,
                'sex' => $sex,
                'num' => $num,
                'post' => $post,
                'parent_id' => $parent_id,
                'department_id' => $department_id,
                'updatetime' => time()
            ]) == false) {
            $this->error('修改失败');
        }
        $this->success('修改成功');
    }

    //审核员工成功
    public function checkStaffSuccess()
    {
        $name = input('name', '');
        $img = input('img', '');
        $email = input('email', '');
        $sex = input('sex', 0);
        $role = input('role', 0);
        $num = input('num', '');
        $post = input('post', '');
        $mobile = input('mobile', '');
        $department_id = input('department_id', 0);
        $parent_id = input('parent_id', 0);
        $id = input('id', 0, 'intval');
        if (StaffModel::where(['id' => $id, 'status' => 0])->find() == false) {
            $this->error('待审核员工不存在');
        }
        if (empty($department_id)) {
            $this->error('请选择员工部门！');
        }
        if (StaffModel::where(['id' => $id])->update([
                'name' => $name,
                'img' => $img,
                'email' => $email,
                'sex' => $sex,
                'num' => $num,
                'role' => $role,
                'mobile' => $mobile,
                'post' => $post,
                'parent_id' => $parent_id,
                'department_id' => $department_id,
                'status' => 1,
                'updatetime' => time()
            ]) == false) {
            $this->error('审核失败');
        }
        $this->success('审核成功');
    }

    //员工绑定信息
    public function staffBind(){
        $staff=StaffModel::get($this->auth->id);
        $data=[
            'is_bind_wechat'=>$staff['openid']?1:($staff['wx_openid']?1:0)
        ];
        $this->success('请求成功',$data);
    }

    //审核员工拒绝
    public function checkStaffError()
    {
        $id = input('id');
        if (StaffModel::where(['id' => $id, 'status' => 0])->find() == false) {
            $this->error('待审核员工不存在');
        }
        if (StaffModel::where(['id' => $id, 'status' => 0])->update(['deletetime' => time()]) == false) {
            $this->error('拒绝失败');
        }
        $this->success('审核成功');
    }

    //获取员工统计
    public function getStaffStatistics()
    {
        $id = input('id');
        if (empty($id)) {
            $this->error('员工不存在');
        }
        $staff=StaffModel::where(['id'=>$id])->find();
        if (empty($staff)) {
            $this->error('员工不存在');
        }
        $times=input('times','');
        if(empty($times)){
            $this->error('筛选时间不能为空');
        }
        if ($times) {//创建时间
            $times          = explode(',', $times);
            $date = strtotime($times[0]);
            //月底
            $endDate = strtotime('+1 month', strtotime(date($times[1] . '-1')));
        }

        //客户
        $customer = Customer::where([
            'create_staff_id' => $id,
            
            'createtime' => ['between', [$date, $endDate]],
        ])->count();
        //订单
        $contract = Contract::where([
            'create_staff_id' => $id,
            
            'createtime' => ['between', [$date, $endDate]],
            'check_status' => 2
        ])->count();
        //订单金额 业绩
        $contract_money = Contract::where([
            'create_staff_id' => $id,
            
            'createtime' => ['between', [$date, $endDate]],
            'check_status' => 2
        ])->sum('money');
        //回款次数
        $receivables = Receivables::where([
            'create_staff_id' => $id,
            
            'createtime' => ['between', [$date, $endDate]],
            'check_status' => 2
        ])->count();
        //回款金额
        $receivables_money = Receivables::where([
            'create_staff_id' => $id,
            
            'createtime' => ['between', [$date, $endDate]],
            'check_status' => 2
        ])->sum('money');
        //拜访次数
        $baifang = \addons\qingdongams\model\Event::where([
            'type'=>3,
            'staff_id' => $id,
            
            'actual_end_time' => ['between', [$date, $endDate]],
            'status'=>2
        ])->count();

        //销售
        $contractAchievement = Achievement::getStaffTeamAchievement(date('Y-m',$date),date('Y-m',$endDate),$staff,1);
        //回款
        $receivablesAchievement = Achievement::getStaffTeamAchievement(date('Y-m',$date),date('Y-m',$endDate),$staff,2);
        //新建工单
        $workorder = Workorder::where([
            
            'create_staff_id' => $id,
            'createtime' => ['between', [$date, $endDate]]
        ])->count();
        //完成工单数量
        $completeWorkorder = Workorder::where([
            'owner_staff_id' => $id,
            'status' => 3,
            'end_time' => ['between', [$date, $endDate]]
        ])->count();
        //好评工单
        $goodWorkorder = Workorder::where([
            'comment_score' => ['in', [4, 5]],
            'owner_staff_id' => $id,
            'status' => 3,
            'end_time' => ['between', [$date, $endDate]]
        ])->count();
        //工单金额
        $workorderMoney = Workorder::where([
            'comment_score' => ['in', [4, 5]],
            'owner_staff_id' => $id,
            'status' => 3,
            'end_time' => ['between', [$date, $endDate]]
        ])->sum('money');

        $this->success('请求成功', [
            'customer' => $customer,//获客数量
            'baifang' => $baifang,//拜访客户次数

            'contract' => $contract,//订单数量
            'receivables' => $receivables,//回款次数

            'contractAchievement' => $contractAchievement ?: 0,//业绩目标
            'contract_money' => $contract_money,//订单金额

            'receivablesAchievement' => $receivablesAchievement ?: 0,//回款目标
            'receivables_money' => $receivables_money,//回款金额

            'workorder' => $workorder,//新建工单
            'completeWorkorder' => $completeWorkorder,//完成工单数量

            'goodWorkorder' => $goodWorkorder,//好评工单
            'workorderMoney' => $workorderMoney,//工单金额

        ]);
    }
    //获取工单报告
    public function getWorkReport(){
        $id = input('id');
        if (empty($id)) {
            $this->error('员工不存在');
        }
        $staff=StaffModel::where(['id'=>$id])->find();
        if (empty($staff)) {
            $this->error('员工不存在');
        }
        $times=input('times','');
        if(empty($times)){
            $this->error('筛选时间不能为空');
        }
        if ($times) {//创建时间
            $times          = explode(',', $times);
            $date = date('Y-m-d',strtotime($times[0])) ;
            //月底
            $endDate = date('Y-m-d',strtotime('+1 month', strtotime(date($times[1] . '-1'))));
        }
        $where=[
            'staff_id' => $id,
            
            'report_date' => ['between', [$date, $endDate]],
        ];
       $workreport= WorkReport::where($where)->paginate();
        $this->success('请求成功',$workreport);
    }
    //获取员工所属客户
    public function getStaffCustomer()
    {
        $id = input('id');
        if (empty($id)) {
            $this->error('员工不存在');
        }
        $limit = input("limit/d", 10);
        $where = [];
        $where['owner_staff_id'] = $id;
        $list = Customer::where($where)->with([
            'ownerStaff',
            'contacts'
        ])->field('id,name,next_time,owner_staff_id,level,follow')->order('id desc')->paginate($limit);

        $this->success('请求成功', $list);
    }

    //获取部门列表
    public function getDepartment()
    {
        $department = AuthGroup::where([])->field('id,name,pid')->select();

        $data = [];
        foreach ($department as $v) {
            $data[$v['pid']][] = $v;
        }

        $data = self::getChilds($data, 0);

        $this->success('请求成功', $data);
    }

    //生成树状结构
    private static function getChilds($data, $pid) {
        $list = [];
        if (isset($data[$pid])) {
            $list = $data[$pid];
            foreach ($list as $k => $v) {
                $v['children'] = self::getChilds($data, $v['id']);
                $list[$k]    = $v;
            }
        }

        return $list;
    }

    //获取部门列表
    public function getDepartmentList()
    {

        $this->success('请求成功', AuthGroup::where([])->select());
    }

    //获取上级列表
    public function getParentList()
    {
        $id = input('id');
        if (empty($id)) {
            $this->error('参数错误');
        }
        $this->success('请求成功', StaffModel::getList( [$id]));
    }

    //获取角色列表
    public function getRoles()
    {
        //1最高管理员 2公司管理员 3部门经理 4部门主管 5组长 6员工
        $roles = [1 => '最高管理员', 2 => '公司管理员', 3 => '部门经理', 4 => '部门主管', 5 => '组长', 6 => '员工'];
        $data = [];
        foreach ($roles as $id => $name) {
            $data[] = ['id' => $id, 'name' => $name];
        }
        $this->success('请求成功', $data);
    }

    //获取用户上级IDs
    public function getStaffIds()
    {
        $type = input('type');
        if (empty($type)) {
            $this->success('请求成功', []);
        }

        $staff = StaffModel::where(['id' => ['in', 1 ]])->field('id,name,img')->select();
        $this->success('请求成功', []);
    }

    //获取员工地理位置
    public function getStaffPosition()
    {
        $list = StaffModel::where([])->field('id,name,nickname,img,post,mobile')->select();
        $list = collection($list)->toArray();
        foreach ($list as $k => $v) {
            $v['signin'] = StaffSignIn::where(['staff_id' => $v['id']])->limit(2)
                ->field('staff_id,location,lng,lat,createtime')->select();

            $list[$k] = $v;
        }

        $this->success('请求成功', $list);
    }


    //获取员工地理
    public function getStaffLocation()
    {
        $staff_id=input('id');
        if(empty($staff_id)){
            $this->error('参数不能为空');
        }
        $list = StaffSignIn::where(['staff_id' =>$staff_id,'lng'=>['neq','']])
            ->field('lng,lat')->order('id desc')->select();

        $this->success('请求成功', $list);
    }

    //获取全部员工地址
    public function getAllStaffLocation(){
        try{
            \think\Db::execute("SET @@sql_mode='';");
            $list = StaffSignIn::where(['lng'=>['neq','']])
                ->field('staff_id,lng,lat')
                ->with(['staff'])->group('staff_id')->order('id desc')->select();
        }catch (Exception $e){

        }

        $this->success('请求成功', $list);
    }
    //禁用账号
    public function disable_user()
    {
        $id = input('id');
        $status = 2;
        $model = new StaffModel();
        $staff = $model->where(['id' => $id])->find();
        if (empty($staff)) {
            $this->error('员工不存在');
        }
        if ($model->isUpdate(true)->save(['id' => $id, 'status' => $status])) {
            $this->success('操作成功');
        }
        $this->error('操作失败');
    }

    //多少任务没有完成，已用时多长时间， 完成了多少任务，用时多长时间，
    //获客多少，签单多少，回款多少，完成工单多少，花费费用多少
    //简报
    public function briefing()
    {
        $showStaffIds = StaffModel::getLowerStaffId();
        $staff = StaffModel::where(['id' => ['in', $showStaffIds]])->with([
            'contract',
            'receivables',
            'customer',
            'visit',
            'workorder',
            'consume',
        ])->select();


        $this->success('请求成功', $staff);
    }

    // 邮箱注册
    public function registerEmail(){
        $email=input('email','');
        $passcode=input('passcode','');
        if(!$email || !$passcode){
            $this->error('邮箱账号或授权码不能为空');
        }
        if (StaffModel::where(['id' => $this->auth->id])->update([
                'email' => $email,
                'email_code' => $passcode,
                'updatetime' => time()
            ]) == false) {
            $this->error('修改失败');
        }
        $this->success('修改成功');
    }

    // 获取邮箱列表
    public function getEmails() {
        $name = input('name','');
        $where['email'] = [['not null',''],['neq',''],'and'];
        if(isset($name) && $name){
            $where['name|nickname'] = ['like','%'.$name.'%'];
        }
        $where['status'] = 1;
        $list = \addons\qingdongams\model\Staff::where($where)->field('id,name,nickname,email')->select();
        $this->success('',$list);
    }

    //直接体验
    public function logintest()
    {
        $staff = StaffModel::where([])->order('id asc')->find();
        if(!$staff){
            $this->error('员工不存在');
        }
        $stafflogin= $this->auth->direct($staff['id']);
        if ($stafflogin) {
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success(__('Logged in successful'), $data);
        } else {
            $this->error('登录失败');
        }

    }

    /**
     * 获取审批列表
     */
    public function getsteplist()
    {
        // record 跟进  contract 订单  consume 费用  receivables 回款 formapproval_1 审批
        $type = input('type');

        //获取审批
        $data = Flow::getsteplist($type);
        if (empty($data)) {
            $this->error('无可用审批流，请联系管理员');
        }
        $this->success('请求成功', $data);
    }
    /**
     * 获取审批详情
     */
    public function getstepdetail()
    {
        // record 跟进  contract 订单  consume 费用  receivables 回款
        $type = input('type');
        $relation_id=input('relation_id');
        $data = Flow::getstepdetail($type,$relation_id);
        if (empty($data)) {
            $this->success('请求成功', []);
        }
        $this->success('请求成功', $data);
    }


}
